using System;
using System.Collections;
using System.Data;
using System.IO;

namespace Portal.Workflow.Model
{
	/// <summary>
	/// Summary description for RoleCls.
	/// </summary>
	public class RoleCls
	{
      	public string RoleId;
        public string RoleCode;
		public string RoleName;
        public string ParentRoleId="";
        public string ProcessType = "to";
        
		public ArrayList ArrayListEmployees = new ArrayList();
        public RoleCls()
        {
        }

        public RoleCls(string _RoleId, string _RoleName, string _ParentRoleId)
		{
			RoleId = _RoleId;
			RoleName = _RoleName;
            ParentRoleId = _ParentRoleId;
		}

		public void AddMemberToRole(EmployeeCls EmployeeCls)
		{
			ArrayListEmployees.Add(EmployeeCls);
		}

        public string GetXml()
        {
            DataSet dsEmployee = new DataSet();
            dsEmployee.Tables.Add("Info");
            dsEmployee.Tables["Info"].Columns.Add("EmployeeId");
            dsEmployee.Tables["Info"].Columns.Add("LoginName");
            dsEmployee.Tables["Info"].Columns.Add("FullName");
            dsEmployee.Tables["Info"].Columns.Add("OfficeCode");
            dsEmployee.Tables["Info"].Columns.Add("ProcessType");
            for (int iIndex = 0; iIndex < ArrayListEmployees.Count; iIndex++)
            {
                EmployeeCls tEmployeeCls = (EmployeeCls)ArrayListEmployees[iIndex];
                dsEmployee.Tables["Info"].Rows.Add(new object[]{
                    tEmployeeCls.EmployeeId,
                    tEmployeeCls.LoginName,
                    tEmployeeCls.FullName,
                    tEmployeeCls.OfficeCode,
                    tEmployeeCls.ProcessType
                });
            }

            DataSet ds = new DataSet();
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("RoleId");
            ds.Tables["Info"].Columns.Add("RoleName");
            ds.Tables["Info"].Columns.Add("ParentRoleId");
            ds.Tables["Info"].Columns.Add("ProcessType");
            ds.Tables["Info"].Columns.Add("Employees");
            ds.Tables["Info"].Rows.Add(new object[]{
                RoleId,RoleName,ParentRoleId,ProcessType,dsEmployee.GetXml()
            });
            return ds.GetXml();
        }

        public void Parse(string XmlData)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(XmlData));

            RoleId = ds.Tables[0].Rows[0]["RoleId"].ToString();
            RoleName = ds.Tables[0].Rows[0]["RoleName"].ToString();
            try
            {
                ParentRoleId = ds.Tables[0].Rows[0]["ParentRoleId"].ToString();
            }
            catch { }
            ProcessType = ds.Tables[0].Rows[0]["ProcessType"].ToString();
            string Employees = ds.Tables[0].Rows[0]["Employees"].ToString();
            DataSet dsEmployee = new DataSet();
            dsEmployee.ReadXml(new StringReader(Employees));
            ArrayListEmployees.Clear();
            for (int iIndex = 0; iIndex < dsEmployee.Tables[0].Rows.Count; iIndex++)
            {
                string EmployeeId = dsEmployee.Tables[0].Rows[iIndex]["EmployeeId"].ToString();
                string LoginName = dsEmployee.Tables[0].Rows[iIndex]["LoginName"].ToString();
                string FullName = dsEmployee.Tables[0].Rows[iIndex]["FullName"].ToString();
                string OfficeCode = dsEmployee.Tables[0].Rows[iIndex]["OfficeCode"].ToString();
                ProcessType = dsEmployee.Tables[0].Rows[iIndex]["ProcessType"].ToString();

                EmployeeCls oEmployeeCls = new EmployeeCls();
                oEmployeeCls.EmployeeId = EmployeeId;
                oEmployeeCls.LoginName = LoginName;
                oEmployeeCls.FullName = FullName;
                oEmployeeCls.OfficeCode = OfficeCode;
                oEmployeeCls.ProcessType = ProcessType;

                ArrayListEmployees.Add(oEmployeeCls);
            }
        }
	}
}
